<?php

class taoartController extends baseController
{
    static protected $mVisitPath = '';//图片访问路径
    static protected $mFilePath = '';
    static protected $mUrlHomepage = '';
    static protected $easyAI = '';
    static protected $citylist;
    public function __construct()
    {
        parent::__construct();

        self::$mFilePath = ROOT_PATH . "exhibits/";
        if(!is_dir(ROOT_PATH.'exhibits')) mkdir(ROOT_PATH.'exhibits');


        //要分析的网站主页
        self::$mUrlHomepage = '';
        self::$easyAI = 0;

        self::$citylist = model('city')->select();

    }

    public function getContent($isreadihstory = NULL, $endtime = 1)
    {

        $this->analysePageOne($endtime);
        if ($isreadihstory) {
            //历史展览多页
            $returnback = 'ok';
            $url_num = 0;
            while ('timeout' != $returnback) {
                $returnback = $this->analyseHistory($url_num, $endtime);
                ++$url_num;
            }
        }
        return null;
    }

    protected function analysePageOne($endtime = 1)
    {
        //最终所得后缀url的前缀
        $url_prefix = 'http://www.taoart.com/information/detail/';
        //要分析的页面
        $url = 'http://www.taoart.com/information/listAjax.htm';

        //以下是获取正在进行的
        $contents = $this->analysePageAjaxNow($url,1);

        //json_decode 第二个参数为默认为false，false返回object，true返回array（），我觉得用数组比较爽
        //var_dump(json_decode($contents,true));die;
        $contents_arr = json_decode($contents,true);
        //得到总页数
        $pageall = $contents_arr['result']['pages'];
        echo 'allpage',$pageall,'<-->';
        $page = 1;
        while ($page <= $pageall){
            echo 'page::',$page,'<-->';

            /*echo '正在进行：：：';
            print_r($pageall);
            echo '<br />';
            break;*/

            $contents = json_decode($this->analysePageAjaxNow($url,$page),true);
            $details = $contents['result']['result'];
            $count = count($details);
            $temp_i = 0;
            echo 'allcount::',$count,'<-->';
            while($temp_i < $count){
                $url_details = $url_prefix.$details[$temp_i]['id'].'.htm';
                echo 'num::',$temp_i;
                if(!$this->analysePageOneExtend($url_details,$endtime)){
                    ++$temp_i;
                    continue;
                }
                ++$temp_i;
            }
            ++$page;
        }
        die;
        //下面是获取即将开始展讯
        $contents = $this->analysePageAjaxFuture($url,1);

        //json_decode 第二个参数为默认为false，false返回object，true返回array（），我觉得用数组比较爽
        //var_dump(json_decode($contents,true));die;
        $contents_arr = json_decode($contents,true);
        //得到总页数
        $pageall = $contents_arr['result']['pages'];
        $page = 1;
        while ($page <= $pageall){

            /*echo '即将开始：：：';
            print_r($pageall);
            echo '<br />';
            break;*/

            $contents = json_decode($this->analysePageAjaxFuture($url,$page),true);
            $details = $contents['result']['result'];
            $count = count($details);
            $temp_i = 0;
            while($temp_i < $count){
                $url_details = $url_prefix.$details[$temp_i]['id'].'.htm';
                if(!$this->analysePageOneExtend($url_details,$endtime)){
                    ++$temp_i;
                    continue;
                }
                ++$temp_i;
            }
            ++$page;
        }


        //以下是获取全部的展览
        //全部展讯只获取前20页，需要了可以自己修改
        $contents = $this->analysePageAjax($url,1);

        //print_r($contents);die;
        //json_decode 第二个参数为默认为false，false返回object，true返回array（），我觉得用数组比较爽
        //var_dump(json_decode($contents,true));die;
        $contents_arr = json_decode($contents,true);

        //print_r($contents_arr);die;
        //得到总页数 这里没有使用,只爬了前20页的
        $pageall = $contents_arr['result']['pages'];
        $pageall = 50;
        $page = 1;
        while ($page <= $pageall){
            $contents = json_decode($this->analysePageAjax($url,$page),true);
            /*echo 'kanhaol:::','<br />';
            print_r($contents);die;*/

            $details = $contents['result']['result'];
            $count = count($details);
            //echo $count;die;
            $temp_i = 0;
            while($temp_i < $count){
                $url_details = $url_prefix.$details[$temp_i]['id'].'.htm';
                //print_r($url_details);die;
                if(!$this->analysePageOneExtend($url_details,$endtime)){
                    ++$temp_i;
                    continue;
                }
                ++$temp_i;
            }
            //die;
            ++$page;
        }


        return null;
    }

    protected function analyseHistory($url_num, $endtime)
    {
        //是否抓取历史展览

        //如果超出所需历史时间则返回'timeout'，否则返回任意值
        return 'is_timeout';
    }

    protected function analysePageOneExtend( $url, $endtime)
    {
        $ishere = model('maexhibitions')->find('url="'.$url.'"');
        if($ishere){
            return true;
        }
        $detailsInfo = array();
        //实现详细页面的抓取及存储
        $contents = file_get_contents($url);
        $pos = mb_strpos($contents,'exRight">');
        if($pos === false){
            return '';
        }

        $contents = mb_substr($contents,$pos+mb_strlen('exRight">'));

        $pos = mb_strpos($contents,'knowledgeTitle">');
        $contents = mb_substr($contents,$pos +mb_strlen('knowledgeTitle">'));
        $pos = mb_strpos($contents,'</h1');
        $detailsInfo['name'] = strip_tags(mb_substr($contents,0,$pos));
        $pos = mb_strpos($contents,'"exDetail1">');
        $contents = mb_substr($contents,$pos + mb_strlen('"exDetail1">'));
        $pos = mb_strpos($contents,'exhibitionTime etColor">');



        $detailsInfo['introduction'] = strip_tags(mb_substr($contents,0,$pos+mb_strlen('exhibitionTime etColor">')));
        $contents = mb_substr($contents,$pos +mb_strlen('exhibitionTime etColor">'));
        $pos = mb_strpos($contents,'tab end -->');
        $contents_temp = mb_substr($contents,0,$pos +mb_strlen('tab end -->'));
        $contents = mb_substr($contents,$pos+mb_strlen('tab end -->'));

        /*
         * 获取示例
         * 展览时间：2017/01/30 — 2017/03/30
         * 展览地点：江苏省苏州市常熟市李闸路90号（九灵犀文化创意园）
         * 展览空间：虞山当代美术馆
         * 参展艺术家：吉赛尔策兰
         * 策展人：张何之
         * 主办单位：虞山当代美术馆
         * 协办单位：大烟囱文化发展有限公司
         */
        //echo $contents_temp;

        $pos_temp = mb_strpos($contents_temp,'展览时间：');
        if($pos_temp !== false){
            //mb_detect_encoding($contents_temp, array('ASCII','UTF-8','GB2312','GBK',));

            $contents_once = mb_substr($contents_temp,$pos_temp+mb_strlen('展览时间：'));
            //print_r($contents_once );
            $pos_temp = mb_strpos($contents_once,'</li');


            $temp_details = strip_tags(mb_substr($contents_once,0,$pos_temp));
            //echo $temp_details;
            //die;
            $temp_details = str_replace(' ','',$temp_details);

            $detailsInfo['opentimestart'] = str_replace('/','-',explode('—',$temp_details)[0]);
            $detailsInfo['opentimeend'] = str_replace('/','-',explode('—',$temp_details)[1]);
            $detailsInfo['opentimestart'] = strtotime($detailsInfo['opentimestart']);
            $detailsInfo['opentimeend'] = strtotime($detailsInfo['opentimeend']);
            //print_r($detailsInfo);die;
        }

        $pos_temp = mb_strpos($contents_temp,'展览地点：');
        if($pos_temp !== false){
            $contents_once = mb_substr($contents_temp,$pos_temp+mb_strlen('四字真言：'));
            $pos_temp = mb_strpos($contents_once,'</li');
            $temp_details = strip_tags(mb_substr($contents_once,0,$pos_temp));
            $temp_details = str_replace(' ','',$temp_details);
            $detailsInfo['address'] = $temp_details;
            $cityid = $this->getCity($temp_details);

            $detailsInfo['city'] = $cityid['name'];
            $detailsInfo['cityid'] = $cityid['id'];
        }

        $pos_temp = mb_strpos($contents_temp,'展览空间：');
        if($pos_temp !== false){
            $contents_once = mb_substr($contents_temp,$pos_temp+mb_strlen('四字真言：'));
            $pos_temp = mb_strpos($contents_once,'</li');
            $temp_details = strip_tags(mb_substr($contents_once,0,$pos_temp));
            $temp_details = str_replace(' ','',$temp_details);
            $detailsInfo['organizers'] = $temp_details;
        }
        $pos_temp = mb_strpos($contents_temp,'主办单位：');
        if($pos_temp !== false){
            $contents_once = mb_substr($contents_temp,$pos_temp+mb_strlen('四字真言：'));
            $pos_temp = mb_strpos($contents_once,'</li');
            $temp_details = strip_tags(mb_substr($contents_once,0,$pos_temp));
            $temp_details = str_replace(' ','',$temp_details);
            $detailsInfo['organizers'] = $temp_details;
        }
        $pos_temp = mb_strpos($contents_temp,'协办单位：');
        if($pos_temp !== false){
            $contents_once = mb_substr($contents_temp,$pos_temp+mb_strlen('四字真言：'));
            $pos_temp = mb_strpos($contents_once,'</li');
            $temp_details = strip_tags(mb_substr($contents_once,0,$pos_temp));
            $temp_details = str_replace(' ','',$temp_details);
            $detailsInfo['coorganizers'] = $temp_details;
        }
        $pos_temp = mb_strpos($contents_temp,'展览空间：');
        if($pos_temp !== false){
            $contents_once = mb_substr($contents_temp,$pos_temp+mb_strlen('四字真言：'));
            $pos_temp = mb_strpos($contents_once,'</li');
            $temp_details = strip_tags(mb_substr($contents_once,0,$pos_temp));
            $temp_details = str_replace(' ','',$temp_details);
            $detailsInfo['organizers'] = $temp_details;
        }
        $pos_temp = mb_strpos($contents_temp,'策展人：');
        if($pos_temp !== false){
            $contents_once = mb_substr($contents_temp,$pos_temp+mb_strlen('三真言：'));
            $pos_temp = mb_strpos($contents_once,'</li');
            $temp_details = strip_tags(mb_substr($contents_once,0,$pos_temp));
            $temp_details = str_replace(' ','',$temp_details);
            $detailsInfo['curator'] = $temp_details;
        }
        $pos_temp = mb_strpos($contents_temp,'参展艺术家：');
        if($pos_temp !== false){
            $contents_once = mb_substr($contents_temp,$pos_temp+mb_strlen('五个字真言：'));
            $pos_temp = mb_strpos($contents_once,'</li');
            $temp_details = strip_tags(mb_substr($contents_once,0,$pos_temp));
            $temp_details = str_replace(' ','',$temp_details);
            $detailsInfo['artist'] = $temp_details;
        }

        $ImgDetails = array();
        $pos = mb_strpos($contents,'title="');
        if($pos !== false){
            $pos = mb_strpos($contents,'src="');
            $contents = mb_substr($contents,$pos+mb_strlen('src="'));

            $pos = mb_strpos($contents,'</div');
            $contents = mb_substr($contents,0,$pos);

            $pos = mb_strpos($contents,'"');
            $detailsInfo['pagehomeimgurl'] = $this->getImgUrl(mb_substr($contents,0,$pos));

            $pos = mb_strpos($contents,'title="');
            $temp_i = 0;
            while($pos !==false){
                $contents = mb_substr($contents,$pos +mb_strlen('title="'));
                $pos = mb_strpos($contents,'"');
                $ImgDetails[$temp_i]['name'] = mb_substr($contents,0,$pos);
                $pos = mb_strpos($contents,'src="');
                $contents = mb_substr($contents,$pos+mb_strlen('src="'));
                $pos = mb_strpos($contents,'"');
                $ImgDetails[$temp_i]['imgurl'] = $this->getImgUrl(mb_substr($contents,0,$pos));
                $pos = mb_strpos($contents,'title="');

                ++$temp_i;
            }
        }



        $detailsInfo['url'] = $url;
        $detailsInfo['introduction'] = $this->keeponeline($detailsInfo['introduction']);
        $exhibitionmaker = new exhibitionsMakerController();
        $detailsInfo['companiesID'] = $exhibitionmaker->getmuseumID($detailsInfo,1);
        $location = $exhibitionmaker->getlocation($detailsInfo);
        $detailsInfo['longitude'] = $location['longitude'];
        $detailsInfo['latitude'] = $location['latitude'];

        $detailsInfo['curatorID'] = $exhibitionmaker->getauthorID($detailsInfo);

        $detailsInfo['visittime'] = time();

        if($detailsInfo['cityid']==null || $detailsInfo['cityid']=='' || $detailsInfo['cityid']==false){
            $detailsInfo['cityid'] = 0;
        }

        if($detailsInfo['opentimestart']<=0){
            $detailsInfo['opentimestart'] = 0;
        }
        if($detailsInfo['opentimeend']<=0){
            $detailsInfo['opentimeend'] = 0;
        }


        //print_r($detailsInfo);
        //echo '挂到存储前';
        $findbyname=model('maexhibitions')->find("name='".$detailsInfo['name']."'");
        //echo '挂到存储后';

        if(!$findbyname) {
            $exhibitsmaker = new exhibitsMakerController();

            $detailsInfo['visittime'] = time();
            $id = model('maexhibitions')->saveData($detailsInfo);

            $path_temp = self::$mFilePath . date('Ymd');
            //echo $path_temp;die;
            if (!is_dir($path_temp)) {
                mkdir($path_temp);
                chmod($path_temp,0777);
            }

            $temp_imgurl_i = 0;
            if(isset($detailsInfo['pagehomeimgurl'])){
                array_splice($ImgDetails,0,0,$detailsInfo['pagehomeimgurl']);
            }
            //print_r($ImgDetails);die;
            while(isset($ImgDetails[$temp_imgurl_i])){

                $imgpath = $this->saveImage($ImgDetails[$temp_imgurl_i]['imgurl']);
                if($imgpath === false){
                    ++$temp_imgurl_i;
                    continue;
                }
                if($imgpath != '') {
                    $imgurl[$temp_imgurl_i]['imgpath'] = 'exhibits/' . date('Ymd') . '/' . $imgpath;
                }else{
                    ++$temp_imgurl_i;
                    continue;
                }

                //print_r($ImgDetails[$temp_imgurl_i]);
                $ret = $exhibitsmaker->changeyachang($ImgDetails[$temp_imgurl_i]);
               // echo '处理后';
                //print_r($ret);die;


                $ImgDetails[$temp_imgurl_i]['exhibitionsID'] = $id;

                model('maexhibits')->saveData($ret);
                /*echo $img_save_return,'<br />';
                var_dump($ImgDetails[$temp_imgurl_i]);die;*/
                ++$temp_imgurl_i;
            }
        }
        return true;
    }

    //专用于淘艺术的ajax 全部展览
    protected function analysePageAjax($url, $page)
    {

        //处理ajax网页
        $ch = curl_init();

        curl_setopt($ch,CURLOPT_URL,$url);

        $referer = 'http://www.taoart.com/information/list.htm';
        $post_details = 'provinceId=-1&cityId=-1&countyId=-1&startTime=&endTime=&type=0&countryType=1&currentDate='.date('Y-m-d').'&page='.$page;
        curl_setopt($ch,CURLOPT_HEADER,0);

        curl_setopt($ch,CURLOPT_REFERER,$referer);

        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

        curl_setopt($ch,CURLOPT_POST,1);

        curl_setopt($ch,CURLOPT_POSTFIELDS,$post_details);

        $contents = curl_exec($ch);
        curl_close($ch);

        return $contents;
    }
    //专用淘艺术的ajax 正在进行展览
    protected function analysePageAjaxNow($url,$page){
        //处理ajax网页
        $ch = curl_init($url);
        $referer = 'http://www.taoart.com/information/list.htm';
        $post_details = 'provinceId=-1&cityId=-1&countyId=-1&startTime=&endTime=&type=1&countryType=1&currentDate='.date('Y-m-d').'&page='.$page;
        curl_setopt($ch,CURLOPT_HEADER,0);
        curl_setopt($ch,CURLOPT_REFERER,$referer);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_POST,1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$post_details);

        $contents = curl_exec($ch);
        curl_close($ch);
        return $contents;


    }

    //专用淘艺术的ajax 即将开始展览
    protected function analysePageAjaxFuture($url,$page){
        //处理ajax网页
        $ch = curl_init($url);
        $referer = 'http://www.taoart.com/information/list.htm';
        $post_details = 'provinceId=-1&cityId=-1&countyId=-1&startTime=&endTime=&type=2&countryType=1&currentDate='.date('Y-m-d').'&page='.$page;
        curl_setopt($ch,CURLOPT_HEADER,0);
        curl_setopt($ch,CURLOPT_REFERER,$referer);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_POST,1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$post_details);

        $contents = curl_exec($ch);
        curl_close($ch);
        return $contents;



    }

    protected function getImgUrl($url){
        //https://img.taoart.com/group1/M00/0A/30/CqjkJ1ij8WOAFYFSAANN3myF3NY796@140_140.jpg   这里是缩略图，要删除@140_140
        $url = str_replace('@140_140','',$url);
        $url = preg_replace("/(\s|\&nbsp\;|　|\xc2\xa0)/", "",$url);
        //echo $url,'<br />';
        return $url;
    }

    protected function saveImage($img_url){
        $folder = ROOT_PATH.'exhibits/';
        //echo $img_url;
        if(!is_dir($folder)){
            mkdir($folder,0777);
            //chmod($folder,0777);
        }
        $folder = $folder.date('Ymd').'/';
        if(!is_dir($folder)){
            mkdir($folder,0777);
            //chmod($folder,0777);
        }

        //echo 'url：',$img_url,'<br />';

        $pic_format = array('.bmp','.jpg','.tiff','.gif','.pcx','.tga','.exif','.jpeg','.png',
            '.fpx','.svg','.psd','.cdr','.pcd','.dxf','.ufo','.eps','.ai','.raw','.WMF');

        //echo 'saveImage:','<br />','imgurl:',$img_url,'<br />','path:',$folder,'<br />';
        $img_url = str_replace('\\','',$img_url);

        $end = explode(".", $img_url);
        $temp_i = 0;
        while(isset($end[$temp_i])){
            ++$temp_i;
        }
        $end = "." . $end[$temp_i-1];

        if(!in_array($end,$pic_format)){
            return false;
        }
        //echo 'url没问题','<br />';
        $ch = curl_init();
        //echo 'curl没问题','<br />';
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        //要头信息
        curl_setopt($ch,CURLOPT_HEADER,1);
        //不要正文
        curl_setopt($ch, CURLOPT_NOBODY, 1);

        curl_setopt($ch,CURLOPT_URL,$img_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $contents = curl_exec($ch);
        //echo 'contents::',$contents,'<br />';

        /*$content_type = curl_getinfo($ch,CURLINFO_CONTENT_TYPE);

        echo $content_type;die;*/
        curl_close($ch);


        $pos = mb_stripos($contents,'200');
        if($pos === false){
            $pos = mb_strpos($contents,'OK');
            if($pos === false){
                return '';
            }
        }

        $pic_name = $this->utime() . rand(1111, 9999) . $end;

        //set_time_limit(8 * 60 * 60); //限制最大的执行时间
        $destination_folder = $folder; //文件下载保存目录
        $newfname = $destination_folder . $pic_name;//文件PATH

        //echo 'newfname:',$newfname,'<br />';

        $ch = curl_init();
        try{$fp = fopen($newfname,'wb');}catch(Exception $err){
            cpError::write($err->getMessage());
            return 0;}
        //echo 'fopen is ok!!';
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch,CURLOPT_URL,$img_url);
        curl_setopt($ch,CURLOPT_FILE,$fp);

        curl_exec($ch);
        curl_close($ch);
        fclose($fp);

        //生成缩略图
        Image::thumb($newfname,$destination_folder.'64_'.$pic_name,'','640','640',6);


        /*$image = new \Think\Image();
        //echo $newfname;
        try{$image->open($newfname);}catch (Exception $err){return 0;}
        echo 'Thinkphp open is right!!!';
        // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
        //echo $destination_folder.'64_'.$pic_name;die;
        try{$image->thumb(640, 640)->save($destination_folder.'64_'.$pic_name);}catch (Exception $err){return 0;}
        //echo 'Thinkphp thumb is right!!','<br />';
        //die;*/

        return $pic_name;
    }


    function utime()
    {
        return time();
    }

    //时间加空格
    protected function gettimestr($str)
    {
        $start = mb_substr($str, 0, 10);
        $end = mb_substr($str, 10, 5);
        $str = $start . ' ' . $end;
        return $str;
    }

    protected function getCity($address){
        if($address == ''){
            return '其他';
        }
       /* $city_list = array('北京',
            '上海',
            '广州',
            '深圳',
            '成都',
            '武汉',
            '杭州',
            '重庆',
            '郑州',
            '南京',
            '西安',
            '苏州',
            '天津',
            '长沙',
            '福州',
            '沈阳',
            '济南',
            '温州',
            '合肥',
            '哈尔滨',
            '其他');*/


        $city_list = self::$citylist;
        //print_r($city_list);die;
        //print_r($city_list);
        $temp_i = 34;
        $pos = mb_strpos($address,$city_list[$temp_i]['name']);
        while($pos===false && isset($city_list[$temp_i]['name'])&& $temp_i<509){
            $pos = mb_strpos($address,$city_list[$temp_i]['name']);
            ++$temp_i;
        }

        $toret = array(
            'id'=>0,
            'name'=>'其他',
            'pid'=>0,
            );

        if($temp_i >508){
            return $toret;
        }
        --$temp_i;
        echo $city_list[$temp_i];

        return isset($city_list[$temp_i])?$city_list[$temp_i]:$toret;
    }

    protected function keeponeline($str){
        //先删空格再把多个换行符替换成一个
        $str = preg_replace ( "/\s(?=\s)/","\\1", $str);

        $search = array(" ");
        $replace = array("");
        $str =  str_replace($search, $replace, $str);

        $search = array("\n","\r","\t");
        $replace = array(" "," "," ");
        $str =  str_replace($search, $replace, $str);

        $str = preg_replace('/\s+/','\n',$str);

        $str = str_replace('&nbsp;','',$str);

        return $str;
    }


}